<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>编辑 EPUB</title>
  <link href="../Styles/styles.css" rel="stylesheet" type="text/css"/>
</head>

<body>
  <h2>高级查找 &amp; 替换——正则表达式</h2>

  <p>The real power of Find &amp; Replace in Sigil is provided by its support of Regular Expressions (also called regex). Regex is basically using patterns instead of plain text for searches to make matching text more flexible.</p>
  <p>在 Sigil 中查找 &amp; 替换的真正功能是由它对正则表达式（也称为regex）的支持提供的。正则表达式基本上是使用模式而不是纯文本进行搜索，以使匹配的文本更加灵活。</p>

  <p>You can use regex in Find &amp; Replace by changing the mode of the search from Normal to Regex.</p>
  <p>通过将搜索模式从“普通”更改为“正则表达式”，可以在“查找 &amp; 替换”中使用正则表达式。</p>

  <div class="image">
    <img alt="tutorial-find-adv-regex" src="../Images/tutorial-find-adv-regex.png"/>
  </div>

  <p>Regex is a very important tool in Sigil, but only a very brief introduction can be given in this tutorial. See the next chapter for a reference list of regular expressions.</p>
  <p>正则表达式是 Sigil 中非常重要的工具，但是在本教程中只能给出非常简短的介绍。有关正则表达式的参考列表，请参见下一章。</p>

  <p>For example, instead of searching for very specific text to replace or delete, such as "Page 123", it would be much more useful to search for text that contains "Page" followed by a number – that’s exactly what regular expressions can provide.</p>
  <p>例如，与其搜索要替换或删除的非常具体的文本（例如“Page 123”），不如搜索包含“Page”后跟数字的文本，这将非常有用，这正是正则表达式可以提供的。</p>

  <p>Instead of putting your actual text into the Find or Replace boxes, you can substitute it with special regex codes. So in the example above you could search for "Page \d+" instead of "Page 123" because "\d+" is a regex code that means any digit (the "\d") repeated at least once (the "+").</p>
  <p>您可以将其替换为特殊的正则表达式代码，而不必将实际文本放入“查找”或“替换”框中。因此，在上面的示例中，您可以搜索“Page \d+”而不是“Page 123”，因为“\d+”是一个正则表达式代码，表示任何数字（“\d”）至少重复一次（“+”）。</p>

  <div class="image">
    <img alt="tutorial-find-adv-page" src="../Images/tutorial-find-adv-page.png"/>
  </div>

  <h3 class="sigil_not_in_toc">使用正则表达式替换文本</h3>

  <p>But even more useful is that you can use regex to help with replacing text as well. In the example above, you might want to keep the page numbers but replace the word "Page". You could use then use something like:</p>
  <p>但更有用的是，您也可以使用正则表达式来帮助替换文本。在上面的示例中，您可能希望保留页码，但替换单词“Page”。您可以使用然后使用类似以下的内容：</p>

  <ul>
    <li><span class="listheading">查找：</span> 

    <pre class="example">Page (\d+)</pre></li>

    <li><span class="listheading">替换：</span> 

    <pre class="example">This is page number \1</pre></li>
  </ul>

  <div class="image">
    <img alt="tutorial-find-adv-replace" src="../Images/tutorial-find-adv-replace.png"/>
  </div>

  <p>This will replace any occurrence of "Page" followed by a number with "This is page" followed by the same number:</p>
  <p>这将用“This is page”后跟相同的数字替换出现的“Page”后跟一个数字：</p>

  <ul>
    <li><span class="listheading">之前：</span> 

    <pre class="example">Page 418</pre></li>

    <li><span class="listheading">之后：</span> 

    <pre class="example">This is page 418</pre></li>
  </ul>

  <p>The first point to note about this replacement is the use of the regex code <span class="example">(\d+)</span> in the Find box. The <span class="example">\d+</span> code tells the search to look for numbers as above. But the use of parenthesis around the code tells the search to remember what those numbers were – to remember anything matched in the parenthesis. The other point to note is the use of the regex code <span class="example">\1</span> in the Replace box, which tells Replace to substitute the characters remembered in the Find statement for the string <span class="example">\1</span> wherever it finds it.</p>
  <p>关于此替换的第一点注意事项是在“查找”框中使用了正则表达式代码 <span class="example">(\d+))</span>。<span class="example">\d+</span> 代码告诉搜索以查找上述数字。但是，在代码周围使用括号可以使搜索记住这些数字是什么——记住括号中匹配的所有内容。还要注意的另一点是在“替换”框中使用了正则表达式代码 <span class="example">\1</span>，它告诉 Replace 将在 Find 语句中记住的字符替换为字符串 <span class="example">\1</span> 在任何找到它的地方。</p>

  <h3 class="sigil_not_in_toc" id="tutorial_regex_formatting">使用正则表达式更改格式</h3>

  <p>As a further example of regex, this is how you might change the formatting of certain text into chapter headings.</p>
  <p>作为正则表达式的另一个示例，这是将某些文本的格式更改为章节标题的方式。</p>

  <p>Let’s say that you have an imported HTML file that contains lots of chapter headings, but none of them are marked using the <span class="example">h1</span> heading tag. Instead they are all marked as paragraphs like this:</p>
  <p>假设您有一个导入的 HTML 文件，其中包含很多章节标题，但是都没有使用 <span class="example">h1</span> 标题标记进行标记。而是将它们全部标记为以下段落：</p>

  <pre class="example">&lt;p&gt;CHAPTER 7&lt;/p&gt;
</pre>

  <p>Assuming every paragraph like this is a chapter heading, you could use this regex:</p>
  <p>假设像这样的每个段落都是一章的标题，则可以使用此正则表达式：</p>

  <ul>
    <li><span class="listheading">查找：</span> 

    <pre class="example">&lt;p&gt;\sCHAPTER\s(\d+)\s&lt;/p&gt;</pre></li>

    <li><span class="listheading">替换：</span> 

    <pre class="example">&lt;h1&gt;Chapter \1&lt;/h1&gt;</pre></li>
  </ul>

  <div class="image">
    <img alt="" src="../Images/tutorial_find_regex_chapter.png"/>
  </div>

  <p>That’s quite a lot to digest, but if you look carefully you can see that it’s very similar to the Page number example above in that it’s remembering the digits in the chapter name and using them in the replace.</p>
  <p>可以消化的东西很多，但是如果仔细看，您会发现它与上面的页码示例非常相似，因为它会记住章节名称中的数字，并在替换中使用它们。</p>

  <p>It’s the Find that is the most interesting. It breaks down like this:</p>
  <p>查找是最有趣的。它像这样分解：</p>

  <ul>
    <li><span class="example">&lt;p&gt;</span> – Look for a starting paragraph tag.</li>
    <li><span class="example">&lt;p&gt;</span>——寻找起始段落标签。</li>

    <li><span class="example">\s</span> – Regex code to match any white space (blanks, tabs, etc.).</li>
    <li><span class="example">\s</span>——正则表达式代码以匹配任何空白（空格，制表符等）。</li>

    <li><span class="example">CHAPTER</span> – Match the word CHAPTER (Regex is case-sensitive by default).</li>
    <li><span class="example">CHAPTER</span>——匹配单词 CHAPTER（正则表达式默认区分大小写）。</li>

    <li><span class="example">(\d+)</span> – Regex code to match any number of digits in a row and remember them.</li>
    <li><span class="example">(\d+)</span>——正则表达式代码可连续匹配任意数量的数字并记住它们。</li>

    <li><span class="example">&lt;/p&gt;</span> – Look for an end paragraph tag.</li>
    <li><span class="example">&lt;/p&gt;</span>——寻找结尾段落标签。</li>
  </ul>

  <p>You could just use a space instead of <span class="example">\s</span> but <span class="example">\s</span> is more flexible since it will match any number of blank spaces and tabs.</p>
  <p>您可以使用空格代替 <span class="example">\s</span>，但是 <span class="example">\s</span> 更为灵活，因为它将匹配任意数量的空格和标签。</p>

  <p>So the results of that search could be:</p>
  <p>因此，该搜索的结果可能是：</p>

  <ul>
    <li><span class="listheading">之前：</span> 

    <pre class="example">&lt;p&gt;  CHAPTER    14&lt;/p&gt;</pre></li>

    <li><span class="listheading">之后：</span> 

    <pre class="example">&lt;h1&gt;Chapter 14&lt;h1&gt;</pre></li>
  </ul>

  <div class="tip">
    <p class="tiptext">It becomes more difficult to use regex if the formatting used in a file is not consistent – in these cases you may have to do several Find &amp; Replace passes and even resort to making the updates one by one manually.</p>
    <p class="tiptext">如果文件中使用的格式不一致，则使用正则表达式将变得更加困难——在这种情况下，您可能必须执行几次“查找 &amp; 替换”过程，甚至不得不手动进行一次手动更新。</p>
  </div>

  <p>This is only a very basic description of what regex is, but it gives you an idea of what is possible. The trick with regex is getting the right find and replace strings, and that only comes from reading, asking questions, and testing.</p>
  <p>这只是什么是正则表达式的非常基本的描述，但是它使您对可能的想法有所了解。regex 的诀窍是找到正确的查找和替换字符串，而这仅来自阅读，提问和测试。</p>

  <h3 class="sigil_not_in_toc">有用的选项</h3>

  <p>You may have noticed the extra Options in Find &amp; Replace: DotAll, Minimal Match, and Auto-Tokenise. All of these options are useful when using Regex. If you are starting out using regex, then the recommendation is to check each of the boxes:</p>
  <p>您可能已经注意到“查找 &amp; 替换”中的其他选项：DotAll，最小匹配和自动标记。使用 Regex 时，所有这些选项都很有用。如果您开始使用正则表达式，则建议选中每个框：</p>

  <div class="image">
    <img alt="tutorial-find-adv-options" src="../Images/tutorial-find-adv-options.png"/>
  </div>

  <p>This is what each of the options is used for:</p>
  <p>这是每个选项的用途：</p>

  <p class="sigil_not_in_toc"></p>

  <ul>
    <li><span class="listheading">DotAll:</span> This regex option prepends (?s) to all regex searches and is used when you want the ".*" pattern to match across separate lines.</li>
    <li><span class="listheading">DotAll：</span>此正则表达式选项在所有正则表达式搜索之前加上 (?s)，当您希望“.*”模式在单独的行中匹配时使用。</li>

    <li><span class="listheading">Minimal Match:</span> This regex option prepends (?U) to all regex searches and is used when you want a pattern to match the shortest occurrence instead of the longest match.</li>
    <li><span class="listheading">最小匹配：</span>此正则表达式选项在所有正则表达式搜索之前加 (?U)，当您希望模式匹配出现次数最短而不是最长匹配的模式时使用。</li>

    <li><span class="listheading">Auto-Tokenise:</span> When using Ctrl-F on selected text to add text to Find &amp; Replace, this will convert spaces to \s and escape certain characters so they are suitable for regex.</li>
    <li><span class="listheading">自动标记：</span>在选定文本上使用 Ctrl-F 将文本添加到“查找 &amp; 替换”中时，这会将空格转换为 \s 并转义某些字符，因此它们适用于正则表达式。</li>
  </ul>

  <p>See the <a href="../Text/tutorial_regex_reference.html">Regular Expression Reference</a> chapter for more details.</p>
  <p>有关更多详细信息，请参见 <a href="../Text/tutorial_regex_reference.html">正则表达式参考</a> 一章。</p>
</body>
</html>